package CountDownLatchDemo;
import java.util.concurrent.CountDownLatch;  
import java.util.concurrent.ExecutorService;  
import java.util.concurrent.Executors;  
  
/*
 * 每调用一次countDown()方法，计数器减1,计数器大于0 时，
 * await()方法会阻塞后面程序执行，直到计数器为0
 * ，await()方法会阻塞后面程序执行，直到计数器为0，await(long timeout, TimeUnit unit)，
 * 是等待一定时间，然后执行，不管计数器是否到0了。
 * */

public class CountDownLatchDemo {  
  
    public static void main(String[] args) {  
        ExecutorService executor = Executors.newCachedThreadPool();  
          
        CountDownLatch latch = new CountDownLatch(3);  
          
        Worker w1 = new Worker(latch,"张三");  
        Worker w2 = new Worker(latch,"李四");  
        Worker w3 = new Worker(latch,"王二");  
          
        Boss boss = new Boss(latch);  
          
        executor.execute(w3);  
        executor.execute(w2);  
        executor.execute(w1);  
        executor.execute(boss);  
          
        executor.shutdown();  
    }  
  
} 